<template>
    <div class="app-container">
        <el-row>
            <el-col :span="24" align="right">
                <el-button type="text" @click="enterCreateInvestigation" style="font-size: 17px;">新建调查期</el-button>
            </el-col>
        </el-row>
        <el-table v-loading="listLoading" :data="viewData" element-loading-text="Loading" border fit highlight-current-row
            :cell-style="{ padding: '5px' }" stripe>
            <el-table-column label="编号" align="center" :min-width="'5%'" :max-width="'5%'">
                <template slot-scope="scope">
                    {{ scope.row.investigationId }}
                </template>
            </el-table-column>
            <el-table-column align="center" label="调查期名称" :min-width="'14%'" :max-width="'14%'">
                <template slot-scope="scope">
                    {{ scope.row.investigationTitle }}
                </template>
            </el-table-column>
            <el-table-column label="提交日期" align="center" :min-width="'7%'" :max-width="'7%'">
                <template slot-scope="scope">
                    {{ scope.row.investigationStartDate }}
                </template>
            </el-table-column>
            <el-table-column label="截止日期" align="center" :min-width="'7%'" :max-width="'7%'">
                <template slot-scope="scope">
                    {{ scope.row.investigationEndDate }}
                </template>
            </el-table-column>
            <!-- <el-table-column label="未上报" align="center" :min-width="'5%'" :max-width="'5%'">
                <template slot-scope="scope">
                    {{ scope.row.noSubmitNum }}
                </template>
            </el-table-column>
            <el-table-column label="未审批" align="center" :min-width="'5%'" :max-width="'5%'">
                <template slot-scope="scope">
                    {{ scope.row.noExamineNum }}
                </template>
            </el-table-column>
            <el-table-column label="已审批" align="center" :min-width="'5%'" :max-width="'5%'">
                <template slot-scope="scope">
                    {{ scope.row.examineNum }}
                </template>
            </el-table-column>
            <el-table-column label="就业减少企业" align="center" :min-width="'7%'" :max-width="'7%'">
                <template slot-scope="scope">
                    {{ scope.row.decreaseNum }}
                </template>
            </el-table-column> -->
            <el-table-column label="描述" align="center" :min-width="'20%'" :max-width="'20%'">
                <template slot-scope="scope">
                    {{ scope.row.investigationDescription == null ? '无' : scope.row.investigationDescription }}
                </template>
            </el-table-column>
            <el-table-column fixed="right" align="center" label="操作" :min-width="'14%'" :max-width="'14%'">
                <template slot-scope="scope">
                    <!-- <el-button @click="jumpToExamine(scope.row)" type="text">审批</el-button> -->
					<el-button @click="uploadData(scope.row.investigationId)" type="text">导出数据</el-button>
                    <el-button @click="enterModifyInvestigation(scope.row)" type="text">编辑</el-button>
                    <el-button @click="deleteInvestigation(scope.row)" type="text">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination @size-change="changePageSize" @current-change="changeCurrentPage" :current-page="page.currentPage"
            :page-sizes="[10, 20, 50]" :page-size="page.pageSize" layout="total, sizes, prev, pager, next, jumper"
            :total="page.totalItems">
        </el-pagination>

        <el-dialog :title="modifying ? '编辑调查期' : '新建调查期'" :visible.sync="creating" width="50%">
            <el-form :model="investigation">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="调查期名称" label-width="100px">
                            <el-input v-model="investigation.investigationTitle" placeholder="请输入调查期名称"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="10">
                        <el-form-item label="上报日期" label-width="100px">
                            <el-date-picker v-model="investigation.investigationStartDate" type="date" placeholder="选择日期"
                                :clearable=false style="width:100%"></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="上报期限" label-width="80px">
                            <el-input-number v-model="investigation.investigationLength" :min="1" size="medium"
                                style="width: 100%;"></el-input-number>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="20">
                        <el-form-item label="调查期描述" label-width="100px">
                            <el-input v-model="investigation.investigationDescription" placeholder="请输入模式描述" type="textarea"
                                :rows="3"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="creating = false">取 消</el-button>
                <el-button type="primary" @click="confirmInvestigation">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import {
	getAllInvestigationInfo,
	deleteInvestigation,
	modifyInvestigation,
	createInvestigation,
	uploadCollectedData
} from '@/api/province/investigationManage'

export default {
    data() {
        return {
            rawData: [],
            viewData: [],
            listLoading: true,
            page: {
                currentPage: 1,
                pageSize: 10,
                totalItems: 0,
            },
            modifying: false,
            creating: false,
            investigation: Object
        }
    },
    created() {
        this.fetchData()
    },
    methods: {
        fetchData() {
            this.listLoading = true
            getAllInvestigationInfo().then(response => {
                response.data.forEach(investigation => {
                    investigation.investigationStartDate = new Date(investigation.investigationStartDate)
                    investigation.investigationEndDate = new Date(investigation.investigationStartDate.getTime() + investigation.investigationLength * 24 * 60 * 60 * 1000).toLocaleDateString()
                    investigation.investigationStartDate = investigation.investigationStartDate.toLocaleDateString()
                })
                this.rawData = response.data.sort((a, b) => a.investigationId - b.investigationId)
                this.page.totalItems = this.rawData.length
                this.display()
                this.listLoading = false
            })
        },

        changePageSize(val) {
            this.page.pageSize = val;
            this.display();
        },

        changeCurrentPage(val) {
            this.page.currentPage = val;
            this.display();
        },

        display() {
            this.viewData = this.rawData.slice((this.page.currentPage - 1) * this.page.pageSize, Math.min(this.page.totalItems, this.page.currentPage * this.page.pageSize))
        },

        deleteInvestigation(investigation) {
            this.$confirm('确定要删除该调查期吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                if (investigation.examineNum + investigation.noExamineNum > 0)
                    this.$message({
                        type: 'warning',
                        message: '已有企业上报数据，无法删除'
                    })
                else
                    deleteInvestigation({ investigationId: investigation.investigationId }).then(response => {
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        })
                        this.fetchData()
                    })
            }).catch(() =>
                this.$message({
                    type: 'info',
                    message: '已取消'
                })
            )
        },

        jumpToExamine(investigation) {
            // TODO
            alert("此时应跳转到 " + investigation.investigationTitle + " 的审批页面")
        },

        enterCreateInvestigation() {
            this.creating = true
            this.modifying = false
            this.investigation = { investigationStartDate: new Date(), investigationLength: 1 }
        },

        enterModifyInvestigation(investigation) {
            this.creating = true
            this.modifying = true
            this.investigation = { ...investigation, investigationLength: 1 }
            this.investigation.investigationStartDate = new Date(this.investigation.investigationStartDate)
            this.investigation.investigationLength = ((new Date(this.investigation.investigationEndDate)).getTime() - this.investigation.investigationStartDate.getTime()) / 24 / 60 / 60 / 1000
        },

        confirmInvestigation() {
            // this.creating = false
            // console.log(this.investigation.investigationLength)

            if (this.investigation.investigationTitle == null || this.investigation.investigationTitle.length == 0)
                this.$message({
                    message: '调查期名称不能为空',
                    type: 'warning'
                })
            else {
                this.investigation.investigationEndDate = (new Date(this.investigation.investigationStartDate.getTime() + this.investigation.investigationLength * 24 * 60 * 60 * 1000)).toLocaleDateString().replace(/\//g, '-')
                this.investigation.investigationStartDate = this.investigation.investigationStartDate.toLocaleDateString().replace(/\//g, '-')
                // console.log(this.investigation.investigationStartDate)
                let action = this.modifying ? modifyInvestigation : createInvestigation
                action(this.investigation).then(() => {
                    this.$message({
                        message: (this.modifying ? '修改' : '新建') + '成功',
                        type: 'success'
                    })
                    this.fetchData()
                })
                this.creating = false
            }
        },

		uploadData(invId){
			uploadCollectedData({investigationId: invId})
		}
    }
}

</script>
